import { defineStore } from 'pinia'
import { ref } from 'vue'

import type { SignData } from '@/apis/model/auth'
import type { UserModel } from '@/apis/model/user'
import { jumpNameAfterLogin, jumpNameAfterSignOut, storesKey } from '@/config'
import i18n from '@/locales'
import router from '@/router'
// import { useRouter } from 'vue-router'

export const useAppStore = defineStore(
  storesKey.app,
  () => {
    const isLogined = ref(true)
    // const router = useRouter()

    const token = ref<string | undefined>('')
    const userInfo = ref<Partial<UserModel>>()
    const request_api = ref<string>('')
    const locale = ref<Language>(storage.get('lang') || 'zh-CN')

    /**
     * 登录
     * @param data 登录参数
     * @returns Promise<boolean>
     */
    const signInAction = (data: SignData): Promise<boolean> => {
      return new Promise((resolve, reject) => {
        // setTimeout 模拟接口请求间隔
        setTimeout(() => {
          signInApi(data)
            .then(res => {
              isLogined.value = true
              userInfo.value = res.user
              token.value = res.token
              setLoadStatus(true)
              router.replace({ name: jumpNameAfterLogin })
              resolve(true)
            })
            .catch(e => {
              console.error(e)
              reject(false)
            })
        }, 300)
      })
    }
    /**
     * 登出
     * @returns Promise<boolean>
     */
    const signOutAction = (flag = true) => {
      function handleClear() {
        // locale.value = 'zh-CN'
        // storage.set('lang', 'zh-CN')
        // i18n.global.locale.value = 'zh-CN'
        isLogined.value = false
        userInfo.value = undefined
        token.value = undefined
        router.replace({ name: jumpNameAfterSignOut })
      }
      if (flag) {
        signOutApi().then(handleClear)
        return
      }
      handleClear()
    }
    // 获取登
    const protocolType = ref(true)
    //设置顶部安全提示
    function setProtocolType(value: boolean) {
      protocolType.value = value
    }
    const sideCollapse = ref(false)
    function toggleSideCollapse(status: boolean) {
      sideCollapse.value = status
    }
    const SET_LOCALE = (lang: Language) => {
      //语言切换
      locale.value = lang
      storage.set('lang', lang)
      i18n.global.locale.value = lang
    }
    //设置使用内部还是外部请求地址
    const setApi = (api: string) => {
      request_api.value = api
    }
    // 获取登录用户信息
    const getUserInfo = async () => {
      // await user_info().then(res => {
      //   userInfo.value = res as IUserInfo
      // })
    }
    //是否第一次加载
    const isPageLoad = ref(true)
    const setLoadStatus = (status: boolean) => {
      isPageLoad.value = status
    }
    const screenType = ref(1) //1:pc 2:ipad 3:mobile
    const setScreenType = (type: number) => {
      screenType.value = type
    }
    return {
      screenType,
      setScreenType,
      isPageLoad,
      setLoadStatus,
      isLogined,
      signInAction,
      signOutAction,
      protocolType,
      setProtocolType,
      sideCollapse,
      toggleSideCollapse,
      locale,
      SET_LOCALE,
      token,
      userInfo,
      request_api,
      setApi,
      getUserInfo
    }
  },
  { persist: true }
)
